<template>
	<view class="padding-top-sm">
		<view class="edgeInsetTop"></view>
		<view class="form margin-lr" v-if="optionData.type == 'accompany' ||optionData.type == 'medicalvisit'">
			<view class="order padding-tb-sm bg-white flex justify-between align-center">
				<view class="left">
					<text class="text-30 text-black">就诊人员</text>
				</view>
				<view class="right flex-sub flex justify-end align-center" @tap="$wanlshop.to(`/pages/user/record/treatment/treatment?source=${typeData[order_type]}&order_id=${optionData.id}`)">
					<text class="name" v-if="treatment">{{treatment.name}}({{sex[treatment.sex]}})（{{treatment.relation_txt}}）</text>
					<text class="placeholder" v-else>请选择</text>
					<text class="wlIcon-fanhui2 margin-left-xs"></text>
				</view>
			</view>
			<view class="order padding-tb-sm bg-white flex justify-between align-center">
				<view class="left">
					<text class="text-30 text-black" style="width: 160rpx;">地址</text>
				</view>
				<view class="right flex-sub flex justify-end align-center" @tap="$wanlshop.to(`/pages/user/record/treatment/treatment?source=${typeData[order_type]}&order_id=${optionData.id}`)">
					<text class="name" v-if="treatment">{{treatment.address_detail}}</text>
					<text class="placeholder" v-else>请选择</text>
					<!-- <text class="wlIcon-fanhui2 margin-left-xs"></text> -->
				</view>
			</view>
			<view class="order padding-tb-sm bg-white flex justify-between align-center">
				<view class="left">
					<text class="text-30 text-black">姓名</text>
				</view>
				<view class="right flex-sub flex justify-end align-center" @tap="$wanlshop.to(`/pages/user/record/treatment/treatment?source=${typeData[order_type]}&order_id=${optionData.id}`)">
					<text class="name" v-if="treatment">{{treatment.name}}</text>
					<text class="placeholder" v-else>请选择</text>
					<!-- <text class="wlIcon-fanhui2 margin-left-xs"></text> -->
				</view>
			</view>
			<view class="order padding-tb-sm bg-white flex justify-between align-center">
				<view class="left">
					<text class="text-30 text-black">联系电话</text>
				</view>
				<view class="right flex-sub flex justify-end align-center" @tap="$wanlshop.to(`/pages/user/record/treatment/treatment?source=${typeData[order_type]}&order_id=${optionData.id}`)">
					<text class="name" v-if="treatment">{{treatment.mobile}}</text>
					<text class="placeholder" v-else>请选择</text>
					<!-- <text class="wlIcon-fanhui2 margin-left-xs"></text> -->
				</view>
			</view>
		</view>
		<view class="form margin-lr" v-else>
			<view class="order padding-tb-sm bg-white flex justify-between align-center" @tap="$wanlshop.to(`/pages/user/envelope/address?source=3&order_id=${optionData.id}&type=${optionData.type}`)">
				<view class="left">
					<text class="text-30 text-black">联系人</text>
				</view>
				<view class="right">
					<text class="name" v-if="addressData">{{addressData.name}}</text>
					<text class="placeholder" v-else>请选择</text>
					<text class="wlIcon-fanhui2 margin-left-xs"></text>
				</view>
			</view>
			<view class="order padding-tb-sm bg-white flex justify-between align-center" @tap="$wanlshop.to(`/pages/user/envelope/address?source=3&order_id=${optionData.id}&type=${optionData.type}`)">
				<view class="left">
					<text class="text-30 text-black">联系电话</text>
				</view>
				<view class="right">
					<text class="name" v-if="addressData">{{addressData.mobile}}</text>
					<text class="placeholder" v-else>请选择</text>
					<!-- <text class="wlIcon-fanhui2 margin-left-xs"></text> -->
				</view>
			</view>
			<view class="order padding-tb-sm bg-white flex justify-between align-center">
				<view class="left">
					<text class="text-30 text-black">服务地址</text>
				</view>
				<view class="right flex-sub flex justify-end align-center" @tap="$wanlshop.to(`/pages/user/envelope/address?source=3&order_id=${optionData.id}&type=${optionData.type}`)">
					<text class="name text-cut-1" style="max-width: 500rpx;text-align: right;" v-if="addressData">{{`${addressData.address_detail}`}}</text>
					<text class="placeholder" v-else>请选择</text>
					<!-- <text class="wlIcon-fanhui2 margin-left-xs"></text> -->
				</view>
			</view>
			<view class="order padding-tb-sm bg-white flex justify-between align-center" @tap="$wanlshop.to(`/pages/user/envelope/address?source=3&order_id=${optionData.id}&type=${optionData.type}`)">
				<view class="left">
					<text class="text-30 text-black">详细地址</text>
				</view>
				<view class="right">
					<text class="name text-cut-1" style="max-width: 500rpx;text-align: right;"  v-if="addressData">{{addressData.address}}</text>
					<text class="placeholder" v-else>请选择</text>
					<!-- <text class="wlIcon-fanhui2 margin-left-xs"></text> -->
				</view>
			</view>
		</view>
		
		<view class="cu-form-group padding-lr-sm margin-lr margin-top-xs radius" v-if="order_type == 'homeking'">
			<view class="title">性别</view>
			<view style="display: none;">
				<input name="gender" :value="genderdata" disabled placeholder-class="placeholder-class"></input> 
			</view>
			<switch class='switch-sex' @change="Gender" :class="gender?'checked':''" :checked="gender?true:false"></switch>
		</view>
		
		<!-- <view class="time bg-white padding-sm padding-tb margin-lr margin-top-xs flex justify-between align-center" v-if="order_type == 'accompany' && is_home == 2">
			<view class="left">
				<text class="text-30 text-black">就诊医院</text>
			</view>
			<view class="right" @tap="$wanlshop.to('/pages/medicalcare/accompany/safeguard?t=1')">
				<text class="text-30" :class="{placeholder:!hospital}">{{hospital || '请选择医院'}}</text>
				<text class="wlIcon-fanhui2 margin-left-xs text-black"></text>
			</view>
		</view> -->
		
		<!-- <wanl-select v-if="order_type == 'accompany' && is_home == 2" :list="hospitalList"
			label-key="title" value-key="id" title="选择所属医院" v-model="hospital" placeholder="请选择所属医院"
			@change="select($event,'hospital_id')" style="margin: 0 30rpx;margin-top: 10rpx;border-radius: 10rpx 10rpx 0 0;padding: 0 20rpx;">
			<view class="title flex align-center">
				<text>所属医院</text>
			</view>
		</wanl-select>
		
		<wanl-select v-if="order_type == 'accompany' && is_home == 2" :list="sectionList"
			label-key="name" value-key="id" title="选择所在科室" v-model="section_name" placeholder="请选择所在科室"
			@change="select($event,'section_ids')" style="margin: 0 30rpx;padding: 0 20rpx;">
			<view class="title flex align-center">
				<text>所在科室</text>
			</view>
		</wanl-select> -->
		
		
		<view class="form margin-lr margin-top-sm">
			<view class="order padding-tb-sm bg-white flex justify-between align-center" v-if="order_type == 'accompany' && is_home == 2">
				<view class="left">
					<text class="text-30 text-black">所属医院</text>
				</view>
				<view class="right flex flex-end align-center flex-sub">
					<input style="width: 100%;" type="text" placeholder="请输入完整医院名称" class="text-right" v-model="hospital_id" />
				</view>
			</view>
			
			<view class="order padding-tb-sm bg-white flex justify-between align-center" v-if="order_type == 'accompany' && is_home == 2">
				<view class="left">
					<text class="text-30 text-black">所在科室</text>
				</view>
				<view class="right flex flex-end align-center flex-sub">
					<input style="width: 100%;" type="text" placeholder="请输入所在科室(妇科,骨科)" class="text-right" v-model="section_id" />
				</view>
			</view>
			
			
			<view class="order padding-tb-sm bg-white flex justify-between align-center" v-if="order_type == 'accompany' && is_home == 2">
				<view class="left">
					<text class="text-30 text-black">具体位置</text>
				</view>
				<view class="right flex flex-end align-center flex-sub">
					<input style="width: 100%;" type="text" placeholder="住院部xx楼xx房间xx房号" class="text-right" v-model="bed_num" />
				</view>
			</view>
		</view>
		
		
		<view class="time bg-white padding-sm padding-tb margin-lr margin-top-xs flex justify-between align-center" style="border-radius: 10rpx;">
			<view class="left">
				<text class="text-30 text-black">服务时间</text>
			</view>
			<!-- <view class="right" @tap="$wanlshop.to('/pages/medicalcare/homemaking/chooseTime')"> -->
			<view class="right" @tap="show = true">
				<text class="text-30" :class="{placeholder:!time}">{{time || '请选择服务时间'}}</text>
				<text class="wlIcon-fanhui2 margin-left-xs text-black"></text>
			</view>
		</view>
		<u-picker v-model="show" mode="time" :params="{ year: true, month: true, day: true, hour: true, minute: true, second: false }" @confirm="confirm"></u-picker>
		<view class="goods bg-white margin-lr margin-top-xs flex justify-between" v-if="goodsData">
			<image :src="$wanlshop.oss(goodsData.image)" mode="aspectFill"></image>
			<view class="info">
				<view class="title text-cut-2">{{goodsData.title}}</view>
				<view class="center flex justify-between align-center">
					<view class="flex justify-start">
						<text class="text-price text-red">{{goodsData.price}}</text>
						<text class="text-red text-sm" v-if="order_type != 'accompany'">/{{goodsData.asprice}}</text>
					</view>
					<uni-number-box :min="order_type == 'homeking' ? goodsData.order_quantity : 1" :value="num" @change="changeNum"></uni-number-box>
				</view>
				<view class="bottom">
					<text>合计</text>
					<text class="text-bold text-red text-price">{{sub_price}}</text>
				</view>
			</view>
		</view>
		
		<view class="bg-white margin-lr margin-tb-sm">
			<textarea class="padding-sm text-df" style="width: 100%;" v-model="memo" placeholder="如有特色要求，请给商家留言" />
		</view>
		
		<!-- <view class="cu-form-group margin-tb-sm margin-lr padding-top-bj">
			<view class="grid col-4 grid-square flex-sub">
				<view class="bg-img" v-for="(image,index) in sendDate.images" :key="index">
				 <image :src="image" @tap="previewImage(sendDate.images, index)" mode="aspectFill"></image>
					<view class="cu-tag bg-red" @tap.stop="close(index)">
						<text class='wlIcon-shanchu2'></text>
					</view>
				</view>
				<view class="solids" @tap="chooseImg" v-if="sendDate.images.length<8">
					<text class="wlIcon-31paishe"></text>
				</view>
			</view>
		</view> -->
		
		<view class="safeAreaBottom"> </view>
		<view class="wanlian cu-bar tabbar solid-top foot text-df">
			<view class="flex justify-start align-center">
				<text class="wanl-gray">待支付</text>
				<text class="text-red" v-if="goodsData">{{ sub_price }}元</text>
			</view>
			<button v-if="loading" class="cu-btn lg bg-green margin-lr-bj">
				<text class="wlIcon-jiazai wlIconfont-spin margin-right-xs"></text> 提交中...
			</button>
			<button v-else @tap="addOrder" class="cu-btn lg bg-green margin-lr-bj text-bold text-lg"> 预约并支付 </button>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			bed_num:'',
			section_ids:'',
			section_name:'',
			sectionList: [],
			hospitalList: [],
			sex:{1:"男",2:'女'},
			order_type: '', // 类型
			loading: false,
			reload: false,
			token: '',
			optionData: {},
			addressData: null,
			orderData: {},
			goodsData: null,
			num: 0,
			sub_price: 0,
			memo: '',
			time: '',
			sendDate: {
			    score: 0,
			    content: "",
				images: [],
			    contact: ""
			},
			gender: true,
			genderdata: 0,
			times: {
				date:'',
				time:'',
				id:''
			},
			treatment:null,
			hospital_id:'',
			hospital: '',
			show: false,
			typeData:{
				medicalvisit:2,
				accompany:3
			},
			is_home: 2 , // 1 不需要  2 需要医院
		};
	},
	onLoad(option) {
		this.optionData = {
			id:option.id,
			address_id:option.address_id,
			type:option.type,
		}
		if(option.is_home) {
			this.is_home = option.is_home
		}
		this.num = option.num
		this.loadData(option);
		this.order_type = option.type;
		this.getHospital()
		
		// 获取缓存中的departmentid值并加载数据
		this.checkAndLoadDepartmentData();
	},
	
	onShow() {
		// 每次显示页面时检查 departmentid 是否有更新
		this.checkAndLoadDepartmentData();
	},
	
	methods: {
		// 检查并加载科室数据
		checkAndLoadDepartmentData() {
			const departmentid = uni.getStorageSync('departmentid');
			if(departmentid) {
				// 请求后端接口获取医院和科室信息
				uni.request({
					url: '/index/peizhenyuyue',
					method: 'GET',
					data: {
						id: departmentid
					},
					success: res => {
						if(res.data) {
							// 将返回值赋给医院和科室
							this.hospital_id = res.data.yiyuan || '';
							// this.hospital_id = res.data.hospital_id || '';
							this.section_id = res.data.keshi || '';
							// this.section_id = res.data.section_id || '';
							this.bed_num = res.data.yiyuan+'-'+res.data.louceng+'-'+res.data.keshi+'-'+res.data.bingshi+'房间';
							console.log('已自动填充医院和科室信息:', res.data);
						}
					}
				});
			}
		},
		
		select(d, name) {
			if(name == 'section_ids') {
				if(!this.hospital_id) {
					this.$wanlshop.msg('请先选择医院');
				} else {
					this.section_id = d.id;
				}
			}
			if (name == 'hospital_id') {
				this.hospital_id = d.id;
				uni.request({
					url: '/addons/internethospital/api.hospital/section',
					method: 'POST',
					data: {
						hospital_id: d.id || this.hospital_id
					},
					success: res => {
						this.sectionList = res.data;
					}
				});
			}
		},
		async getHospital() {
			await uni.request({
				url: '/addons/internethospital/api.hospital/lists',
				method: 'POST',
				data: {
					city_id: uni.getStorageSync('cityid')
				},
				success: res => {
					console.log(res)
					this.hospitalList = res.data.data;
				}
			});
		},
		confirm(data) {
			console.log(data)
			this.time = `${data.year}-${data.month}-${data.day} ${data.hour}:${data.minute}`;
		},
		reloads(item) {
			this.hospital_id = item.id;
			this.hospital = item.title
		},
		getUsers(data) {
			this.treatment = data;
		},
		Gender(e) {
			if(e.detail.value == true){
				this.gender = true;
				this.genderdata = 0;
			}else{
				this.gender = false;
				this.genderdata = 1;
			}
		},
		setData(date,time,timestr,id) {
			this.times = {date:date,time:time,id:id}
			this.time = timestr
		},
		async getAddress(option) {
			this.optionData.address_id = option
			await uni.request({
				url: `/member/address/detail`,
				method:"POST",
				data: {
					id:option
				},
				success: res => {
					this.addressData = res.data;
				}
			})
		},
		async loadData(option) {
			//加载商品
			if(option.type == 'homeking') {
				await uni.request({
					url: '/addons/homeking/api.goods/detail',
					data: {
						id:option.id
					},
					success: res => {
						this.goodsData = res.data;
						// this.goodsData.title = res.data.sku[option.cur].name;
						this.goodsData.price = res.data.sku[option.cur].price;
						this.goodsData.asprice = res.data.sku[option.cur].asprice;
						this.goodsData.sku_id = res.data.sku[option.cur].id
						this.sub_price = this.$wanlshop.bcmul(this.goodsData.price, this.num);
					}
				});
				// await uni.request({
				// 	url: '/publics/address/getaddress',
				// 	success: res => {
				// 		if(res.data.data.length > 0) {
				// 			this.addressData = res.data.data[0]
				// 			this.optionData.address_id = res.data.data[0].id
				// 		}
				// 	}
				// });
			} else if(option.type == 'medicalvisit') {
				await uni.request({
					url: `${this.$config.vehicleurl}/addons/medicalvisit/api.Product/detail`,
					data: {
						product_id:option.id
					},
					success: res => {
						this.goodsData = res.data;
						this.goodsData.asprice = '';
						this.sub_price = this.$wanlshop.bcmul(res.data.price, this.num);
					}
				});
				
				// await uni.request({
				// 	url: `/member/family/lists`,
				// 	method: 'POST',
				// 	data:{
				// 		page:1
				// 	},
				// 	success: res => {
				// 		this.treatment = res.data.data[0]
				// 	}
				// });
			} else if(option.type == 'accompany') {
				// await uni.request({
				// 	url: `/member/family/lists`,
				// 	method: 'POST',
				// 	data:{
				// 		page:1
				// 	},
				// 	success: res => {
				// 		this.treatment = res.data.data[0]
				// 	}
				// });
				await uni.request({
					url: `${this.$config.vehicleurl}/addons/accompany/api.Product/detail`,
					data: {
						id: option.id
					},
					success: res => {
						this.goodsData = res.data;
						this.goodsData.asprice = res.data.price;
						this.sub_price = this.$wanlshop.bcmul(this.goodsData.price, this.num);
					}
				});
			}
			if(option.address_id == 0) return false;
			await uni.request({
				url: `/member/address/detail`,
				method:"POST",
				data: {
					id:option.address_id
				},
				success: res => {
					this.addressData = res.data;
				}
			})
			
		},
		// 提交订单
		async addOrder() {
			console.log(this.times)
			let address_id = this.optionData.address_id;
			if(address_id == 0 && this.optionData.type == 'homeking'){
				this.$wanlshop.msg('请选择地址')
				return false;
			}
			if(!this.treatment && this.optionData.type == 'accompany') {
				this.$wanlshop.msg('请选择人员')
				return false;
			}
			if(!this.hospital_id && this.optionData.type == 'accompany' && this.is_home == 2) {
				this.$wanlshop.msg('请选择就诊医院')
				return false;
			}
			if(!this.section_id && this.optionData.type == 'accompany' && this.is_home == 2) {
				this.$wanlshop.msg('请选择就诊科室')
				return false;
			}
			if(!this.bed_num && this.optionData.type == 'accompany' && this.is_home == 2) {
				this.$wanlshop.msg('请输入床号')
				return false;
			}
			if(!this.time) {
				this.$wanlshop.msg('请选择服务时间')
				return false;
			}
			console.log(this.time)
			this.loading = true;
			
			let datas = await uni.request({url: '/publics/commons/getToken',method: 'POST',data:{type:`${this.order_type}_order_add`}})
			// 提交订单数据data
			if(this.order_type == 'medicalvisit') {
				await uni.request({
					url: `${this.$config.vehicleurl}/addons/medicalvisit/api.Order/addOrder`,
					method: 'POST',
					data: {
						address_id: this.treatment.id,
						memo: this.memo,
						num: this.num,
						product_id: this.optionData.id,
						reserve_time: this.time,
						token: datas[1].data
					},
					success: res => {
						this.$wanlshop.to(`/pages/user/money/pay?type=medicalvisit&token=&id=${res.data.list}`);
						this.loading = false;
					},
					complete: () => {
						setTimeout(() => {
							this.loading = false;
						}, 1500);
					}
				});
			} else if(this.order_type == 'homeking') {
				let time = this.time
				await uni.request({
					url: `/addons/homeking/api.order/add`,
					method: 'POST',
					data: {
						address_id: this.optionData.address_id,
						remark: this.memo,
						quantity: this.num,
						serves_time: this.time,
						// time_slot: this.times.id,
						token: datas[1].data,
						sex: this.genderdata + 1,
						sku_id: this.goodsData.sku_id,
						goods_id: this.optionData.id
					},
					success: res => {
						this.$wanlshop.to(`/pages/user/money/pay?type=homeking&token=${res.data.token}&id=${res.data.order_id}`);
						this.loading = false;
					},
					complete: () => {
						setTimeout(() => {
							this.loading = false;
						}, 1500);
					}
				});
			} else if(this.order_type == 'accompany') {
				console.log({
					product_id: this.optionData.id,
					num: this.num,
					service_time: this.time,
					memo: this.memo,
					treatment_id:this.treatment.id,
					hospital_id:this.hospital_id,
					section_id:this.section_id,
					bed_num:this.bed_num,
					city_id:uni.getStorageSync('cityid'),
					token: datas[1].data
				})
				await uni.request({
					url: `${this.$config.vehicleurl}/addons/accompany/api.Order/addOrder`,
					method: 'POST',
					data: {
						product_id: this.optionData.id,
						num: this.num,
						service_time: this.time,
						memo: this.memo,
						treatment_id:this.treatment.id,
						hospital_id:this.hospital_id,
						section_id:this.section_id,
						bed_num:this.bed_num,
						city_id:uni.getStorageSync('cityid'),
						token: datas[1].data
					},
					success: res => {
						this.$wanlshop.to(`/pages/user/money/pay?type=accompany&id=${res.data.list}`);
						this.loading = false;
					},
					complete: () => {
						setTimeout(() => {
							this.loading = false;
						}, 1500);
					}
				});
			}
		},
		// 修改数量
		changeNum(num){
			console.log(num)
			num = num == 0 ? 1 : num;
			this.num = num;
			// 计算价格
			this.sub_price = this.$wanlshop.bcmul(this.goodsData.price, num);
		},
		close(e){
		    this.sendDate.images.splice(e,1);
		},
	}
};
</script>

<style lang="less">
.wanlian,.safeAreaBottom {
	height: calc(128rpx + env(safe-area-inset-bottom)) !important;
}
.wanlian {
	justify-content: space-between !important;
	background-color: #fff !important;
	> view {
		margin-left: 34rpx;
		text {
			display: inline-block;
			font-size: 32rpx;
			font-family: PingFang SC-Medium, PingFang SC;
			color: #333333;
			line-height: 44rpx;
			font-weight: 400;
			&:nth-child(1) {
				color: #333333;
			}
			&:nth-child(2) {
				color: red;
			}
		}
	}
	button {
		width: 326rpx;
		height: 72rpx;
		border-radius: 10rpx;
	}
}
.goods {
	padding: 26rpx 18rpx;
	border-radius: 10rpx;
	> image {
		width: 202rpx;
		height: 202rpx;
		border-radius: 6rpx;
	}
	.info {
		width: calc(100% - 234rpx);
		.title {
			font-size: 30rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: 400;
			color: #333333;
			line-height: 48rpx;
		}
		.center {
			font-size: 40rpx;
			line-height: 42rpx;
			margin-top: 18rpx;
			> text {
				color: #E31F1F;
				// font-weight: bold;
				font-size: 40rpx;
			}
			/deep/.uni-numbox {
				border: none;
				padding: 0;
				justify-content: center;
				align-items: center;
				.uni-numbox__minus,.uni-numbox__plus {
					background-color: transparent;
					width: 22rpx;
					text {
						display: inline-block;
						color: #000000;
					}
				}
				.uni-numbox__value {
					width: 42rpx;
					height: 42rpx;
					background: #F4F4F4;
					border-radius: 4rpx;
					margin: 0 10rpx;
				}
			}
		}
		.bottom {
			display: flex;
			justify-content: flex-end;
			align-items: center;
			margin-top: 38rpx;
			text {
				display: inline-block;
				&:nth-child(1) {
					font-size: 20rpx;
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 500;
					color: #666666;
					line-height: 28rpx;
				}
				&:nth-child(2) {
					font-size: 34rpx;
					font-family: DIN-Bold, DIN;
					font-weight: bold;
					color: #E31F1F;
					line-height: 48rpx;
					margin-left: 12rpx;
				}
			}
		}
	}
}

.form {
	padding: 0 20rpx;
	background-color: #fff;
	border-radius: 10rpx;
	.order {
		border-bottom: 1rpx solid rgba(182,182,182,0.3);
		&:last-child {
			border: none;
		}
		.right {
			text {
				&.name {
					font-family: PingFang SC-Medium, PingFang SC;
					font-weight: 400;
					color: #444444;
				}
			}
			input {
				color: #222;
				font-size: 28rpx;
			}
			.placeholder-class {
				font-size: 28rpx;
				color: #999;
			}
		}
	}
}
	.switch-sex {
		transform:scale(0.7);
	}
</style>
